import dayjs from "dayjs"

import { TimeRange } from "@/utils"

const useDateTimeFormat = (tms: TimeRange | undefined, name: string[], ms?: boolean, format: string = "YYYY-MM-DD HH:mm:ss") => {
	const msFormat = ms ? (format.includes("HH") ? ".SSS" : ".000") : ""
	const endMsFormat = ms ? (format.includes("HH") ? ".999" : ".000") : ""

	return tms
		? {
				[name[0]]: dayjs(tms[0]).format(`${format}${msFormat}`),
				[name[1]]: dayjs(tms[1]).format(`${format}${endMsFormat}`)
		  }
		: {}
}

export const useDateTimestampFormat = (tms: TimeRange | undefined, name: string[]) => {
	return tms
		? {
				[name[0]]: dayjs(tms[0]).valueOf(),
				[name[1]]: dayjs(tms[1]).valueOf()
		  }
		: {}
}

export default useDateTimeFormat
